package com.vc.aha_server_event.controller;

import com.vc.aha_server_event.annotation.RequestLimit;
import com.vc.aha_server_event.entity.SearchPreferences;
import com.vc.aha_server_event.entity.TravelThroughPage;
import com.vc.aha_server_event.service.SearchPreferencesService;
import com.vc.aha_server_event.service.TravelThroughProjectNumService;
import com.vc.aha_server_event.service.TravelTroughPageService;
import org.bson.types.ObjectId;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 埋点保存业务类
 *
 * @author Vc ,pcy
 * @date 2022/03/30 14:59
 */
@RestController
@RequestMapping("/eventSave")
public class EventSaveController {
    @Resource
    private TravelTroughPageService travelTroughPageService;
    @Resource
    private TravelThroughProjectNumService travelThroughProjectNumService;
    @Resource
    private SearchPreferencesService searchPreferencesService;

    /**
     * 测例，往mongo、redis、mysql中保存数据
     *
     * @return
     */
    @PostMapping("/insertTravelThroughPage")
    public String insertTravelThroughPage(){
        Date date = new Date();

        TravelThroughPage other = TravelThroughPage.builder()
                .leaveTime(date)
                .eventId(date.hashCode())
                .accessTime(date)
                .pageSchema("1234")
                .sourcePageSchema("1234")
                .time(date)
                .otherInfo("other")
                .timeCount(13)
                .userId(123).build();
        ObjectId objectId = travelTroughPageService.addTravelThroughPage(other);
        return "ID after insert: " + objectId;
    }

    /**
     * 写入用户浏览项目数量埋点
     *
     * @return
     */
    @RequestLimit
    @PostMapping("/travelThroughProjectNum")
    public void saveTravelThroughProjectNum(@RequestParam Integer eventId,
                          @RequestParam String timeStamp,
                          @RequestParam Integer userId,
                          @RequestParam Integer projectId
    ){

        travelThroughProjectNumService.saveTravelProjectNum(eventId,timeStamp,userId,projectId);

    }

    /**
     * 写入大家都在搜埋点
     *
     * @return
     */
    @RequestLimit
    @PostMapping("/searchPreferences")
    public void saveSearchPreferences(@RequestParam Integer eventId,
                                      @RequestParam String timeStamp,
                                      @RequestBody SearchPreferences searchPreferences
                                      ){

        searchPreferencesService.saveSearchPreferences(eventId,timeStamp,searchPreferences);

    }
}
